
#include "StateShop.h"
#include "StateShopAnimals.h"
#include "StateShopUpgrades.h"
#include "StatePopUp.h"
#include "StateCoinsPopUp.h"

#include "StateMainMenu.h"
#include "StateManager.h"
#include "InputManager.h"
#include "AudioUtils.h"
#include "ProgressManager.h"

#define ITEMS_PER_ROW        3

void StateShopUpgrades::loadContent()
{
    StateShopCommon::loadContent();
    
    m_oTitleText->setText("Upgrades");
    
    for(int i=0; i<UPGRADES_COUNT; ++i)
    {
        Vector2 vPos = Vector2(120 + 200 * (i % ITEMS_PER_ROW), 690 - 200 * (int)(i / ITEMS_PER_ROW));

        UpgradeData data = ProgressManager::instance().getUpgradeData(i);
        
        m_oUpgradeIcon[i] = new RenderableEntity(data.sIcon, vPos);
        m_oUpgradeIcon[i]->setCentered();
        m_oUpgradeIcon[i]->setScale(Vector2(0.9f, 0.9f));
        
        m_oUpgradeText[i] = new TextEntity(data.sCaption, FONT_NAME, 48, Vector2(320, 350), 1, 1);
        m_oUpgradeText[i]->setCenter(Vector2(0.5f, 0.5f));
        m_oUpgradeText[i]->setShadow(true, COLOR_WHITE);
        m_oUpgradeText[i]->setColor(COLOR_BLACK);
        m_oUpgradeText[i]->setRender(false);

        m_oUpgradePrice[i] = new TextEntity(getPrizeStr(data.iPrize), FONT_NAME, 24, vPos + Vector2(50, -70), 2, 1);
        m_oUpgradePrice[i]->setCenter(Vector2(1.0f, 0.5f));
        m_oUpgradePrice[i]->setShadow();

        m_oUpgradeCoinIcon[i] = new RenderableEntity("icon_babycoin", vPos + Vector2(50, -70));
        m_oUpgradeCoinIcon[i]->setCenter(Vector2(0.0f, 0.5f));
        m_oUpgradeCoinIcon[i]->setScale(Vector2(0.3f, 0.3f));

        m_oEnter->addInEffect(m_oUpgradeIcon[i]);
        m_oEnter->addInEffect(m_oUpgradePrice[i]);
        m_oEnter->addInEffect(m_oUpgradeCoinIcon[i]);
        m_oEnter->addInEffect(m_oUpgradeText[i]);
        
        m_oExit->addOutEffect(m_oUpgradeIcon[i]);
        m_oExit->addOutEffect(m_oUpgradePrice[i]);
        m_oExit->addOutEffect(m_oUpgradeCoinIcon[i]);
        m_oExit->addOutEffect(m_oUpgradeText[i]);
    }
    
    m_oUpgradeDescription = new TextEntity(" ", FONT_NAME, 32, Vector2(320, 320), 1, 0, true, 600);
    m_oUpgradeDescription->setCenter(Vector2(0.5f, 1.0f));
    m_oUpgradeDescription->setShadow();
    
    m_oSelect = new TextEntity("Select an upgrade!", FONT_NAME, 48, Vector2(320, 320), 1, 0, true, 600);
    m_oSelect->setCenter(Vector2(0.5f, 1.0f));
    m_oSelect->setShadow();
    
    m_oSelectedMark = new RenderableEntity("halo", Vector2(0.0f, 0.0f));
    m_oSelectedMark->setCentered();
    m_oSelectedMark->setScale(Vector2(1.1f, 1.1f));
    m_oSelectedMark->setLiving(true);

    m_oBuyIcon = new RenderableEntity("MENU_0005_dialog2", Vector2(320, 100));
    m_oBuyIcon->setCentered();
    m_oBuyIcon->setScale(Vector2(0.7f, 0.7f));
    
    m_oBuyText = new TextEntity("Buy it!", FONT_NAME, 40, Vector2(320, 100), 1, 1);
    m_oBuyText->setCentered();
    m_oBuyText->setShadow();
    m_oBuyText->setColor(COLOR_PINK_TEXT);

    m_iSelected = -1;
    
    m_oEnter->addInEffect(m_oUpgradeDescription);
    m_oEnter->addInEffect(m_oSelect);
    m_oEnter->addInEffect(m_oSelectedMark);
    m_oEnter->addInEffect(m_oBuyIcon);
    m_oEnter->addInEffect(m_oBuyText);
    
    m_oExit->addOutEffect(m_oUpgradeDescription);
    m_oExit->addOutEffect(m_oSelect);
    m_oExit->addOutEffect(m_oSelectedMark);
    m_oExit->addOutEffect(m_oBuyIcon);
    m_oExit->addOutEffect(m_oBuyText);
}

void StateShopUpgrades::unloadContent()
{
    StateShopCommon::unloadContent();
}

void StateShopUpgrades::activated()
{    
    StateShopCommon::activated();

}

void StateShopUpgrades::enter()
{
    switch(m_ePopupState)
    {
        case SHOPPOPUP_NONE:
            m_iSelected = -1;
            break;
            
        case SHOPPOPUP_BUY:
            if(StatePopUp::instance().getResult())
            {
                //Bought the upgrade!
                SaveGame sSaveGame = ProgressManager::instance().getSavedData();
                int iPrice = ProgressManager::instance().getUpgradeData(m_iSelected).iPrize;
                sSaveGame.iCoins -= iPrice;
                sSaveGame.iCoinsSpent += iPrice;
                sSaveGame.baUpgrades[m_iSelected] = true;
                ProgressManager::instance().setSavedData(sSaveGame);
            }
            break;
            
        default:
            break;
    }
    
    StateShopCommon::enter();
    
    bool bCanBuy = m_iSelected != -1 && ProgressManager::instance().getUpgradeData(m_iSelected).bEnabled && !ProgressManager::instance().getSavedData().baUpgrades[m_iSelected] && (!ProgressManager::instance().getUpgradeData(m_iSelected).bBlocked || ProgressManager::instance().getSavedData().baUpgrades[ProgressManager::instance().getUpgradeData(m_iSelected).eBlocked]);
    m_oBuyIcon->setColor(bCanBuy ? COLOR_WHITE : COLOR_GREY);

    for(int i=0; i<UPGRADES_COUNT; ++i)
    {
        m_oUpgradeText[i]->setRender(i == m_iSelected);
        
        bool bGot = ProgressManager::instance().getSavedData().baUpgrades[i];
        
        m_oUpgradeIcon[i]->setColor(bGot ? COLOR_WHITE : COLOR_GREY);
        //m_oUpgradeTickIcon[i]->setRender(bGot);
        m_oUpgradeCoinIcon[i]->setRender(!bGot);
        m_oUpgradePrice[i]->setRender(!bGot);
    }
}

void StateShopUpgrades::buyClicked()
{
    //Buy popup
    char buff[100];
    UpgradeData data = ProgressManager::instance().getUpgradeData(m_iSelected);
    if(ProgressManager::instance().getSavedData().iCoins >= data.iPrize)
    {
        sprintf(buff, "Buy %s for %s coins?", data.sCaption.c_str(), getPrizeStr(data.iPrize).c_str());
        m_ePopupState = SHOPPOPUP_BUY;
        StatePopUp::instance().popupConfirm(buff);
    }
    else 
    {
        StatePopUp::instance().popupConfirm("You don't have enough coins! Do you want to get some more coins?");
        m_ePopupState = SHOPPOPUP_NOCOINS;
    }
}

void StateShopUpgrades::update(float dt)
{
    StateShopCommon::update(dt);

    switch(m_eState)
    {
        case SHOP_IDLE:
        {
            bool bCanBuy = m_iSelected != -1 && ProgressManager::instance().getUpgradeData(m_iSelected).bEnabled && !ProgressManager::instance().getSavedData().baUpgrades[m_iSelected] && (!ProgressManager::instance().getUpgradeData(m_iSelected).bBlocked || ProgressManager::instance().getSavedData().baUpgrades[ProgressManager::instance().getUpgradeData(m_iSelected).eBlocked]);

            if(m_oBack->isClicked())
            {
                m_oExit->start(true);
                m_eState = SHOP_EXIT;
                m_oNextState = &StateShop::instance();
            }
            else if(bCanBuy && m_oBuyIcon->isClicked())
            {
                buyClicked();
            }
            else
            {
                for(int i=0; i<UPGRADES_COUNT; ++i)
                {
                    if(m_oUpgradeIcon[i]->isClicked())
                    {
                        if(i != m_iSelected)
                        {
                            if(m_iSelected != -1)
                                m_oUpgradeText[m_iSelected]->setRender(false);
                            
                            m_iSelected = i;
                            m_oSelectedMark->setPosition(m_oUpgradeIcon[i]->getPosition() +  Vector2(-2, 18));
                            
                            m_oUpgradeText[m_iSelected]->setRender(true);
                            m_oUpgradeDescription->setText(ProgressManager::instance().getUpgradeData(i).sDescription);
                            
                            bool bCanBuy = !ProgressManager::instance().getSavedData().baUpgrades[m_iSelected] && ProgressManager::instance().getUpgradeData(m_iSelected).bEnabled && (!ProgressManager::instance().getUpgradeData(m_iSelected).bBlocked || ProgressManager::instance().getSavedData().baUpgrades[ProgressManager::instance().getUpgradeData(m_iSelected).eBlocked]);
                            m_oBuyIcon->setColor(bCanBuy ? COLOR_WHITE : COLOR_GREY);
                        }
                        else if(bCanBuy)
                        {
                            buyClicked();
                        }
                        break;
                    }
                }
            }
            
            break;
        }
        default:
            break;
    }
}

void StateShopUpgrades::render()
{
    StateShopCommon::render();
    
    for(int i=0; i<UPGRADES_COUNT; ++i)
    {
        m_oUpgradeIcon[i]->render();
        m_oUpgradeText[i]->render();

        m_oUpgradeCoinIcon[i]->render();
        m_oUpgradePrice[i]->render();
    }

    if(m_iSelected != -1)
    {
        m_oSelectedMark->render();
        m_oUpgradeDescription->render();
        
        m_oBuyIcon->render();
        m_oBuyText->render();
    }
    else 
    {
        m_oSelect->render();
    }
}

void StateShopUpgrades::exit()
{
    StateShopCommon::exit();
}

void StateShopUpgrades::setBackState()
{
    m_oNextState = &StateShop::instance();
}
